home *** CD-ROM | disk | FTP | other *** search
/ START Magazine / START VOL 4 NO 1.st / POGOSRC.ARC / CRPARSE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-11-20  |  7.1 KB  |  441 lines

  1.  
  2. /* crparse.c - Code to help parse a creature.  */
  3.  
  4. #include <stdio.h>
  5. #include "pogo.h"
  6.  
  7. int creature_count;
  8.  
  9. /* Is this a system maintained creature local variable? */
  10. constant_secret()
  11. {
  12. if (cttype == TOK_CID)
  13.     {
  14.     if (!only_in_creature(csym->name))
  15.         return(1);
  16.     code_num(OP_LVAR, (NUMBER)-SECRETS+CID);
  17.     }
  18. else if (cttype == TOK_CAGE)
  19.     {
  20.     if (!only_in_creature(csym->name))
  21.         return(1);
  22.     code_num(OP_LVAR, (NUMBER)-SECRETS+CAGE );
  23.     }
  24. else if (cttype == TOK_CNEW)
  25.     {
  26.     if (!only_in_creature(csym->name))
  27.         return(1);
  28.     code_num(OP_LVAR, (NUMBER)-SECRETS+CNEW );
  29.     }
  30. else if (cttype == TOK_CX)
  31.     {
  32.     if (!only_in_creature(csym->name))
  33.         return(1);
  34.     code_num(OP_LVAR, (NUMBER)-SECRETS+CX );
  35.     }
  36. else if (cttype == TOK_CY)
  37.     {
  38.     if (!only_in_creature(csym->name))
  39.         return(1);
  40.     code_num(OP_LVAR, (NUMBER)-SECRETS+CY );
  41.     }
  42. else if (cttype == TOK_CDX)
  43.     {
  44.     if (!only_in_creature(csym->name))
  45.         return(1);
  46.     code_num(OP_LVAR, (NUMBER)-SECRETS+CDX );
  47.     }
  48. else if (cttype == TOK_CDY)
  49.     {
  50.     if (!only_in_creature(csym->name))
  51.         return(1);
  52.     code_num(OP_LVAR, (NUMBER)-SECRETS+CDY );
  53.     }
  54. else
  55.     return(0);
  56. return(1);
  57. }
  58.  
  59. /* Writing to a system maintained creature local variable? */
  60. secret_assignment()
  61. {
  62. static Symbol fake;
  63. char nbuf[10];
  64.  
  65. if (cttype == TOK_CX)
  66.     {
  67.     if (!only_in_creature(csym->name))
  68.         return(1);
  69.     fake.doff = -SECRETS+CX;
  70.     }
  71. else if (cttype == TOK_CY)
  72.     {
  73.     if (!only_in_creature(csym->name))
  74.         return(1);
  75.     fake.doff = -SECRETS+CY;
  76.     }
  77. else if (cttype == TOK_CDX)
  78.     {
  79.     if (!only_in_creature(csym->name))
  80.         return(1);
  81.     fake.doff = -SECRETS+CDX;
  82.     }
  83. else if (cttype == TOK_CDY)
  84.     {
  85.     if (!only_in_creature(csym->name))
  86.         return(1);
  87.     fake.doff = -SECRETS+CDY;
  88.     }
  89. else
  90.     return(0);
  91. strcpy(nbuf, ctoke);
  92. fake.name = nbuf;
  93. fake.scope = LOCAL;
  94. add_statement();
  95. most_of_assignment(&fake);
  96. return(1);
  97. }
  98.  
  99. /* Code in an evolution call */
  100. get_evolve()
  101. {
  102. if (!not_in_creature("Evolve()"))
  103.     return;
  104. if (!eat_token("(") )
  105.     return;
  106. if (!eat_token(")") )
  107.     return;
  108. code_void(OP_EVOLVE);
  109. }
  110.  
  111. /* Make a symbol for a creature forward reference */
  112. Symbol *
  113. make_fcreature(name)
  114. char *name;
  115. {
  116. Symbol *cs;
  117.  
  118. if ((cs = new_symbol(ctoke, FCREATURE, GLOBAL, rframe)) == NULL)
  119.     return(NULL);
  120. if ((cs->symval.p = beg_zero(sizeof(struct func_frame))) == NULL)
  121.     {
  122.     freemem(cs);
  123.     return(NULL);
  124.     }
  125. cs->doff = creature_count++;
  126. return(cs);
  127. }
  128.  
  129. extern do_cread(), do_cwrite();
  130.  
  131. /* Grab the first 3 parameters for a Cread or Cwrite */
  132. crw_params3()
  133. {
  134. Symbol *cs;
  135.  
  136. if (!eat_token(LPAREN_STR))
  137.     return;
  138. if (!need_token())
  139.     return;
  140. if (cttype == TOK_UNDEF)
  141.     {
  142.     if ((csym = make_fcreature(ctoke)) == NULL)
  143.         return;
  144.     cttype = TOK_VAR;
  145.     }
  146. if (cttype == TOK_VAR)
  147.     {
  148.     if (csym->type == CREATURE || csym->type == FCREATURE)
  149.         {
  150.         goto creatureok;
  151.         }
  152.     }
  153. want_creature();
  154. return;
  155.  
  156. creatureok:
  157. cs = csym;
  158. code_num(OP_CON, cs->doff);    /* push creature type */
  159. if (!eat_token(","))
  160.     return;
  161. if (!need_token())
  162.     return;
  163. if (cs->type == FCREATURE)
  164.     {
  165.     add_crw_fix(cs,ctoke);
  166.     code_num(OP_CON, 0);
  167.     }
  168. else
  169.         /* push data offset of local */
  170.     code_num(OP_CON,get_ldoff(cs->symval.p,ctoke));    
  171. if (got_stop)
  172.     return;
  173. if (!eat_token(","))
  174.     return;
  175. get_iexpress();    /* push the creature id */
  176. }
  177.  
  178. /* parse out and code up a cread function */
  179. get_cread()
  180. {
  181. crw_params3();
  182. if (got_stop)
  183.     return;
  184. if (!eat_token(RPAREN_STR))
  185.     return;
  186. if (got_stop)
  187.     return;
  188. finish_fp3(do_cread, OP_PREDEFL);
  189. }
  190.  
  191. /* parse out and code up a cwrite function */
  192. get_cwrite()
  193. {
  194. crw_params3();
  195. if (got_stop)
  196.     return;
  197. if (!eat_token(","))
  198.     return;
  199. get_iexpress();
  200. if (got_stop)
  201.     return;
  202. if (!eat_token(RPAREN_STR))
  203.     return;
  204. if (got_stop)
  205.     return;
  206. code_big(OP_PREDEF, do_cwrite);
  207. code_num(OP_MOVES, (NUMBER)-5);
  208. }
  209.  
  210. /* get data  offset of creature local variable */
  211. get_ldoff(fuf, name)
  212. struct func_frame *fuf;
  213. char *name;
  214. {
  215. Symbol *s;
  216.  
  217. if (((s = in_list(fuf->symbols, name)) == NULL) || s->type != INT)
  218.     {
  219.     expecting_got("creature local variable", name);
  220.     return(0);
  221.     }
  222. return(s->doff);
  223. }
  224.  
  225. extern fclosestt();
  226. extern int get_ta;
  227.  
  228. /* parse out a ClosestT() call */
  229. get_closestt()
  230. {
  231. char cname[80];
  232. Symbol *cs;
  233.  
  234. strcpy(cname, ctoke);
  235. if (!eat_token(LPAREN_STR))
  236.     return;
  237. if (!need_token())
  238.     return;
  239. if (cttype == TOK_UNDEF)
  240.     {
  241.     if ((cs = make_fcreature(cname)) == NULL)
  242.         goto gotsym;
  243.     }
  244. else if (cttype == TOK_VAR)
  245.     {
  246.     if (csym->type == FCREATURE || csym->type == CREATURE)
  247.         {
  248.         cs = csym;
  249.         goto gotsym;
  250.         }
  251.     else
  252.         {
  253.         want_creature();
  254.         return;
  255.         }
  256.     }
  257. else
  258.     {
  259.     want_creature();
  260.     return;
  261.     }
  262. gotsym:
  263. code_num(OP_CON, cs->doff);
  264. if (!eat_token(","))
  265.     return;
  266. get_iexpress();
  267. if (got_stop)
  268.     return;
  269. if (!eat_token(","))
  270.     return;
  271. get_iexpress();
  272. if (got_stop)
  273.     return;
  274. if (!eat_token(RPAREN_STR))
  275.     return;
  276. get_ta = 1;
  277. finish_fp3(fclosestt, OP_PREDEF);
  278. }
  279.  
  280. finish_fp3(f,op)
  281. function *f;
  282. int op;
  283. {
  284. code_big(op, f);
  285. code_num(OP_RETRIEVE, (NUMBER)-3);
  286. #ifdef LATER
  287. code_num(OP_MOVES, (NUMBER)-2);
  288. #endif LATER
  289. }
  290.  
  291.  
  292. /* parse out a spawn call */
  293. get_spawn()
  294. {
  295. int stype;
  296. Symbol *crsym;
  297. struct func_frame *fuf;
  298. int i;
  299.  
  300. if (!eat_token("(") )
  301.     return;
  302. if (!need_token())
  303.     return;
  304. if (cttype == TOK_UNDEF)
  305.     {
  306.     if ((csym = make_fcreature(ctoke)) == NULL)
  307.         return;
  308.     cttype = TOK_VAR;
  309.     }
  310. if (cttype == TOK_VAR)
  311.     {
  312.     crsym = csym;
  313.     stype = crsym->type;
  314.     if (stype == FCREATURE)
  315.         {
  316.         add_fff(crsym);
  317.         code_big(OP_CON, crsym->symval.p); /* push creatures func_frame */
  318.         }
  319.     else if (stype == CREATURE)
  320.         {
  321.         code_big(OP_CON, crsym->symval.p); /* push creatures func_frame */
  322.         }
  323.     else
  324.         {
  325.         spawn_usage();
  326.         return;
  327.         }
  328.     }
  329. else
  330.     {
  331.     spawn_usage();
  332.     return;
  333.     }
  334. if (!need_token())
  335.     {
  336.     return;
  337.     }
  338. if (cttype != ',')
  339.     {
  340.     spawn_usage();
  341.     return;
  342.     }
  343. i = 3;
  344. while (--i >= 0)
  345.     {
  346.     get_iexpress();
  347.     if (got_stop)
  348.         {
  349.         spawn_usage();
  350.         return;
  351.         }
  352.     if (!need_token())
  353.         {
  354.         return;
  355.         }
  356.     if (cttype != ',')
  357.         {
  358.         spawn_usage();
  359.         return;
  360.         }
  361.     }
  362. get_iexpress();
  363. if (got_stop)
  364.     return;
  365. if (!eat_token(")") )
  366.     {
  367.     return;
  368.     }
  369. code_void(OP_SPAWN);
  370. }
  371.  
  372. /* Go define a creature */
  373. get_creature()
  374. {
  375. Symbol *sym;
  376. struct func_frame *fuf;
  377.  
  378. if (in_loop)
  379.     {
  380.     say_fatal("No creatures inside loops, whiles, or fors");
  381.     return;
  382.     }
  383. if (in_func)
  384.     {
  385.     say_fatal("No creatures inside function declarations");
  386.     return;
  387.     }
  388. if (in_creature)
  389.     {
  390.     say_fatal("No creatures inside other creatures");
  391.     return;
  392.     }
  393. if (!need_token())
  394.     return;
  395. if (cttype == TOK_VAR && csym->type == FCREATURE)
  396.     sym = csym;
  397. else
  398.     {
  399.     if (cttype != TOK_UNDEF)
  400.         {
  401.         redefined(ctoke);
  402.         return;
  403.         }
  404.     if ((sym = make_fcreature(ctoke)) == NULL)
  405.         return;
  406.     }
  407. if (!eat_token(LBRACE_STR))
  408.     return;
  409. new_frame();
  410. in_creature = 1;
  411. get_states();
  412. code_void(OP_END);
  413. fixup_fref();
  414. if (got_stop)
  415.     return;
  416. in_creature = 0;
  417. if (!eat_token(RBRACE_STR))
  418.     {
  419.     return;
  420.     }
  421. fuf = sym->symval.p;
  422. save_function(sym, fuf);
  423. fuf->crtype = sym->doff;
  424. sym->type = CREATURE;
  425. fuf->got_strings = got_strings(fuf->symbols);
  426. old_frame();
  427. }
  428.  
  429. got_strings(s)
  430. Symbol *s;
  431. {
  432. while (s != NULL)
  433.     {
  434.     if (s->type == STRING)
  435.         return(1);
  436.     s = s->next;
  437.     }
  438. return(0);
  439. }
  440.  
  441.